package br.com.canvasForAll.ws.resource;

import java.util.Date;

import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import br.com.canvasForAll.model.entity.Perfil;
import br.com.canvasForAll.model.entity.Usuario;
import br.com.canvasForAll.model.services.interfaces.PerfilService;
import br.com.canvasForAll.model.services.interfaces.UsuarioService;
import br.com.canvasForAll.model.utils.json.JSONObject;

@Path("perfil")
public class PerfilWS {
	
	@EJB private PerfilService perfilService;
	@EJB private UsuarioService usuarioService;

	/**
	 * @param [JSON] {"idMobile":"valor", "nome":"valor", "dataCriacao":"", "dataUltimaAtualizacao":"valor"}
	 * @return Response {"idIcs":"valor", "dataUltimaNotificacao":"valor"}
	 * */
	@POST
	@Path("/addPerfil")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response addPerfil(Perfil p) throws Exception{
		try{
			Response resp = null;
			if(p != null){
				if(verificaCamposObrigatorios(p)){
					p.setDataUltimaNotificacao(new Date());
					p = perfilService.salvarPerfil(p);
					
					JSONObject json = new JSONObject();
					json.put("idIcs", p.getId());
					json.put("dataUltimaNotificacao", p.getDataUltimaNotificacao().getTime());
					
					resp = Response.status(Status.OK).entity(json.toString()).build();

				} else 
					resp = Response.status(Status.BAD_REQUEST).build();
			} else
				resp = Response.status(Status.BAD_REQUEST).build();
			
			return resp;

		} catch (Exception e) {
			e.printStackTrace();
			throw new WebApplicationException(Status.NOT_FOUND);
		}
	}
	
	/**
	 * @param [JSON] {"idMobile":"valor", "nome":"valor", "dataCriacao":"", "dataUltimaAtualizacao":"valor"}
	 * @param String id_usuario
	 * @return Response {"idIcs":"valor", "dataUltimaNotificacao":"valor"}
	 * */
	@POST
	@Path("/addPerfil/{id_usuario}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response addPerfilComUsuario(@PathParam("id_usuario") String idUser, Perfil p) throws Exception{
		try{
			Response resp = null;
			
			if(p != null && idUser != null){
				Usuario user = usuarioService.obterId(new Long(idUser));
				
				if(verificaCamposObrigatorios(p) && user != null && user.getId() != null){
					p.setDataUltimaNotificacao(new Date());
//					p.setUsuario(user);
					p = perfilService.salvarPerfil(p);

					JSONObject json = new JSONObject();
					json.put("idIcs", p.getId());
					json.put("dataUltimaNotificacao", p.getDataUltimaNotificacao().getTime());
					
					resp = Response.status(Status.OK).entity(json.toString()).build();
					
				} else 
					resp = Response.status(Status.BAD_REQUEST).build();
			} else
				resp = Response.status(Status.BAD_REQUEST).build();
			
			return resp;
		} catch (Exception e) {
			throw new WebApplicationException(Status.NOT_FOUND);
		}
	}
	
	private boolean verificaCamposObrigatorios(Perfil p) {
		if(p.getNome() != null && p.getDataCriacao() != null && p.getDataUltimaAtualizacao() != null){
			return true;
		} else {
			return false;
		}
	}
}
